import {
  SetMetadata,
  createParamDecorator,
  applyDecorators,
} from '@nestjs/common';

export const RequireLogin = () => SetMetadata('require-login', true);
export const RequireAdmin = () => SetMetadata('require-admin', true);
export const RequireUser = () => SetMetadata('require-user', true);

export const CheckAdmin = () => {
  return applyDecorators(RequireLogin(), RequireAdmin());
};

export const CheckUser = () => {
  return applyDecorators(RequireLogin(), RequireUser());
};

export const UserInfo = createParamDecorator((data, ctx) => {
  const request = ctx.switchToHttp().getRequest();
  if (!request.user) return null;

  return data ? request.user[data] : request.user;
});
